package com.taldh.algorithm.base;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class ListNode {

    public int val;
    public ListNode next;
    public ListNode(int x) {
        val = x;
        next = null;
    }

    public ListNode(int[] nums) {
        ListNode target = new ListNode(nums[0]);
        ListNode head = target;
        for (int i = 1; i < nums.length; i++) {
            target.next = new ListNode(nums[i]);
            target = target.next;
        }
        this.val = head.val;
        this.next = head.next;
    }

    public static boolean isEquals(ListNode node1, ListNode node2) {
        while (node1 != null) {
            if (node2 == null) {
                return false;
            }
            if (node1.val != node2.val) {
                return false;
            }
            node1 = node1.next;
            node2 = node2.next;
        }
        return true;
    }

    @Override
    public String toString() {
        List<Integer> integers = new ArrayList<>();
        ListNode head = this;
        while (head != null) {
            integers.add(head.val);
            head = head.next;
        }
        return integers.toString();
    }
}
